package com.atguigu.gmall.all.controller;

import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.list.SearchParam;
import com.atguigu.gmall.model.list.SearchResponseVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
public class ListController {
    @Autowired
    private ListFeignClient listFeignClient;

    //https://search.jd.com/search?keyword=手机&qrst=1&wq=%E5%A4%A7%E7%B1%B3&stock=1&stoc1&ev=exbrand_%E9%87%91%E9%BE%99%E9%B1%BC%5E&cid3=2676
    //开始搜索
    @GetMapping("/list.html")
    public String list(Model model, SearchParam searchParam){
        //调用远程接口
        SearchResponseVo responseVo = listFeignClient.search(searchParam);
        //1.回显关键词
        model.addAttribute("searchParam",searchParam);
        //2.回显品牌集合
        model.addAttribute("trademarkList",responseVo.getTrademarkList());
        //3.urlParam
        String urlParam = buildUrlParam(searchParam);
        model.addAttribute("urlParam",urlParam);
        //4.平台属性
        model.addAttribute("attrsList",responseVo.getAttrsList());
        //5.商品集合
        model.addAttribute("goodsList",responseVo.getGoodsList());
        //6.总页数 和当前页
        model.addAttribute("totalPages",responseVo.getTotalPages());
        model.addAttribute("pageNo",responseVo.getPageNo());
        //7.排序
        Map orderMap = buildOrderMap(searchParam);
        model.addAttribute("orderMap",orderMap);
        //8.条件回显
        //8.1 显示品牌
        String trademark = searchParam.getTrademark();
        if (!StringUtils.isEmpty(trademark)){
            model.addAttribute("trademarkParam","品牌:"+trademark.split(":")[1]);
        }
        //8.2 显示平台属性
        List<Map> propsParamList = buildPropsParamList(searchParam);
        model.addAttribute("propsParamList",propsParamList);
        return "list/index";
    }

    //8.2 显示平台属性
    private List<Map> buildPropsParamList(SearchParam searchParam) {
        //平台属性数组
        String[] props = searchParam.getProps();
        if (null != props && props.length > 0){
            List<Map> list = Arrays.stream(props).map((prop) -> {
                //props=23:4G:运行内存
                Map map = new HashMap();
                String[] split = prop.split(":");
                map.put("attrId",split[0]);
                map.put("attrValue",split[1]);
                map.put("attrName",split[2]);
                return map;
            }).collect(Collectors.toList());
            return list;
        }
        return null;
    }

    //7.排序
    private Map buildOrderMap(SearchParam searchParam) {
        Map map = new HashMap();
        // order=1:asc  排序规则   2:asc
        String order = searchParam.getOrder();
        if (!StringUtils.isEmpty(order)){
            String[] split = order.split(":");
            map.put("type",split[0]);
            map.put("sort",split[1]);
        }else {
            //默认排序
            map.put("type","1");
            map.put("sort","desc");
        }
        return map;
    }

    //3.追加url
    private String buildUrlParam(SearchParam searchParam) {
        StringBuilder sb = new StringBuilder();
        String keyword = searchParam.getKeyword();
        //1.keyWord
        sb.append("http://list.gmall.com/list.html?keyword="+keyword);
        //2.品牌  品牌的Id:品牌的名称
        String trademark = searchParam.getTrademark();
        if (!StringUtils.isEmpty(trademark)){
            sb.append("&trademark=" + trademark);
        }
        //3.平台属性
        String[] props = searchParam.getProps();
        if (null != props && props.length > 0) {
            for (String prop : props) {
                sb.append("&props" + prop);
            }
        }
        //4.一二三级分类暂时不写了
        return sb.toString();
    }
}
